#include <iostream>
#include <fstream>
#include <unordered_map>
#include "Log.hpp"


using std::cout;
using std::cin;
using std::endl;

const std::string path = "./dict.txt";

class Init
{
private:
    Log log;
    std::unordered_map<std::string, std::string> dict;

public:
    static void SplictStr(std::string line, std::string* part1, std::string* part2, std::string sep)
    {
        int pos = line.find(sep);
        if(pos != std::string::npos)
        {
            *part1 = line.substr(0,pos);
            *part2 = line.substr(pos+1);
        }
    }

    //将对应文件的内容加载进字典
    Init()
    {
        std::ifstream in(path);
        if(!in.is_open())
        {
            log(Fatal, "open file fail: %s ", path.c_str());
            exit(1);
        }

        std::string line;
        while(std::getline(in, line))
        {
            std::string part1, part2;
            //切割字符串, 并将切好的字符放进part1与part2
            SplictStr(line, &part1, &part2, ":");

            //将对应的key, val插进dict
            dict.insert({part1,part2});
        }

        in.close();
    }

    std::string Translation(const std::string& key)
    {
        //查找key
        auto it = dict.find(key);

        if(it != dict.end())
            return dict[key];
        else 
            return "None";
    }

    ~Init(){}
};


